package com.jprocms.module.cms.dal.mysql.investigateopinion;

import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.mybatis.core.mapper.BaseMapperX;
import com.jpro.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.jprocms.module.cms.controller.admin.investigateopinion.vo.InvestigateOpinionListReqVO;
import com.jprocms.module.cms.controller.admin.investigateopinion.vo.InvestigateOpinionPageReqVO;
import com.jprocms.module.cms.dal.dataobject.investigateopinion.InvestigateOpinionDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.lang.Nullable;

import java.util.Date;
import java.util.List;

/**
 * 问卷调查提交反馈 Mapper
 *
 * @author jprocms
 */
@Mapper
public interface InvestigateOpinionMapper extends BaseMapperX<InvestigateOpinionDO> {

    /**
     * 根据主键获取数据
     *
     * @param id 主键ID
     * @return 实体对象。没有找到数据，则返回 {@code null}
     */
    @Nullable
    InvestigateOpinionDO select(Long id);

    /**
     * 根据调查问卷ID删除数据
     *
     * @param investigateId 调查问卷ID
     * @return 被删除的数据条数
     */
    int deleteByInvestigateId(Long investigateId);

    /**
     * 根据用户ID删除数据
     *
     * @param userId 用户ID
     * @return 被删除的数据条数
     */
    int deleteByUserId(Long userId);

    /**
     * 根据站点ID删除数据
     *
     * @param siteId 站点ID
     * @return 被删除的数据条数
     */
    int deleteBySiteId(Long siteId);

    /**
     * 统计意见数量，用于判断是否已经参与过调查。
     *
     * @param investigateId 调查问卷ID
     * @param date          查询日期大于这个时间
     * @param userId        用户ID
     * @param ip            IP地址
     * @param cookie        cookie标识
     * @return 数据数量
     */
    int countBy(@Nullable @Param("investigateId") Long investigateId,
                @Nullable @Param("create_time") Date date,
                @Nullable @Param("userId") Long userId,
                @Nullable @Param("ip") String ip,
                @Nullable @Param("cookie") Long cookie);

    default PageResult<InvestigateOpinionDO> selectPage(InvestigateOpinionPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<InvestigateOpinionDO>()
                .eqIfPresent(InvestigateOpinionDO::getSiteId, reqVO.getSiteId())
                .eqIfPresent(InvestigateOpinionDO::getInvestigateId, reqVO.getInvestigateId())
                .eqIfPresent(InvestigateOpinionDO::getUserId, reqVO.getUserId())
                .eqIfPresent(InvestigateOpinionDO::getIp, reqVO.getIp())
                .eqIfPresent(InvestigateOpinionDO::getCookie, reqVO.getCookie())
                .betweenIfPresent(InvestigateOpinionDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(InvestigateOpinionDO::getId));
    }

    default List<InvestigateOpinionDO> selectList(InvestigateOpinionListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<InvestigateOpinionDO>()
                .eqIfPresent(InvestigateOpinionDO::getSiteId, reqVO.getSiteId())
                .eqIfPresent(InvestigateOpinionDO::getInvestigateId, reqVO.getInvestigateId())
                .eqIfPresent(InvestigateOpinionDO::getUserId, reqVO.getUserId())
                .eqIfPresent(InvestigateOpinionDO::getIp, reqVO.getIp())
                .eqIfPresent(InvestigateOpinionDO::getCookie, reqVO.getCookie())
                .betweenIfPresent(InvestigateOpinionDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(InvestigateOpinionDO::getId));
    }

}
